import bot from "../model/bot/amesu";
import { MsgAction } from "../shared/master/MsgAction";
import { Tpl_positon, client_body_position } from "../shared/master/word/PtlPosition";
import { style_default } from "../style/default";
/**
 * 
 当前位置:测试[3-79]
    ↑
←  🌏  🈲
   🈲
┄══⚔战场#1⚔═━┄
[1]普生
[2]丧尸
┄══⚔战场#2⚔═━┄
[3]普生
[4]丧尸
┄══发现敌人═━┄
[5]普生
 */
export default function (data: MsgAction) {
    let _data = data.data as Tpl_positon;
    let style = new style_default();
    style.setTitle('当前位置')
    style.addLine(`🌏${_data.name}[${_data.x}-${_data.y}]`)
    style.addLine(`▶世界轮回者${_data.player_cont}人`)
    style.setline_Title('可移动方位','🧙')
    style.addLine(`                ${_data.cell_top ? ' 上' : '🌲'}`)
    style.addLine(`          ${_data.cell_left ? ' 左' : '🌲'}  🌏  ${_data.cell_right ? '右' : '🌲'}`)
    style.addLine(`                ${_data.cell_down ? ' 下' : '🌲'}`)

    const battles: { [key: string]: client_body_position[] } = {};
    const enemyPositions: client_body_position[] = [];
    let indexMap: Map<string, number> = new Map()
    let idx = 0;
    _data.bodys.forEach((position, _index) => {
        idx += 1;
        if (!position) {
            return
        }
        indexMap.set(position.uuid, idx)
        if (position.battle_id) {
            if (!battles[position.battle_id]) {
                battles[position.battle_id] = [];
            }
            battles[position.battle_id].push(position);
        } else {
            enemyPositions.push(position);
        }
    });

    Object.keys(battles).forEach((battleId, index) => {
        style.addLine(`┄══⚔战场#${index + 1}⚔═━┄`)
        battles[battleId].forEach((position, index) => {
            let idx = indexMap.get(position.uuid);
            if (position.life_per <= 0) {
                style.addLine(`[${idx + 1}]${position.name}☠️`)
            } else {
                style.addLine(`[${idx + 1}]${position.name}❤${(position.life_per * 100).toFixed(2)}%`)
            }
        });
    });


    if (enemyPositions.length > 0) {
        style.setline_Title('位置信息','🔎')
        if (_data.giftBox) {
            style.addLine(`🔎发现${_data.giftBox}个神秘宝箱[打开宝箱]`)
        }
        if(_data.water){
            style.addLine(`💧在这里有${_data.water}m²的水域，你可以[钓鱼]试试`)
        }
        if(!_data.cell_top || !_data.cell_down || !_data.cell_left || !_data.cell_right){
            style.addLine(`🌲周围有障碍物,你可以[砍树]试试`)
        }

        let arry = [...enemyPositions.values()];
        let start = 0;
        let stateIcon = '';
        for (let i = 1; i < arry.length; i++) {
            if (arry[i].name !== arry[i - 1].name) {
                let position = arry[i - 1]
                let stateIcon = ''
                if (position.life_per == 1) {
                } else if (position.life_per <= 0) {
                    stateIcon = '☠️'
                } else {
                    stateIcon = `❤${(position.life_per * 100).toFixed(2)}%`
                }
                if (start === i - 1) {
                    style.addLine(`[${start + 1}]${position.name}${stateIcon}`)
                } else {
                    style.addLine(`[${start + 1}-${i}]${position.name}${stateIcon}`)
                }
                start = i;
            }
        }
        if (start === arry.length - 1) {
            let position = arry[arry.length - 1]
            let stateIcon = '';
            if (position.life_per == 1) {
            } else if (position.life_per <= 0) {
                stateIcon = '☠️'
            } else {
                stateIcon = `❤${(position.life_per * 100).toFixed(2)}%`
            }
            style.addLine(`[${start + 1}]${arry[arry.length - 1].name}${stateIcon}`)
        } else {
            let position = arry[arry.length - 1]
            let stateIcon = '';
            if (position.life_per == 1) {
            } else if (position.life_per <= 0) {
                stateIcon = '☠️'
            } else {
                stateIcon = `❤${(position.life_per * 100).toFixed(2)}%`
            }
            style.addLine(`[${start + 1}-${arry.length}]${arry[arry.length - 1].name}${stateIcon}`)
        }
    }
    style.help(`位置帮助`)
    // for (let index = 0; index < _data.bodys.length; index++) {
    //     const e = _data.bodys[index];
    //     if (e) {
    //         style.addLine(`[${index + 1}]${e.name}`)
    //     }
    // }
    bot.sendText(data.tolocation, style.end());

}